<?php

namespace mz\models;

use Yii;
use yii\imagine\Image;
use Imagine\Image\ManipulatorInterface;

/**
 * 图片处理类
 * Class ImageModel
 * @package common\models
 */
class ImageModel
{

    /**
     * 解析结果参数
     * [
     *      ["dirname"]=>
     *      string(10) "file/image"
     *      ["basename"]=>
     *      string(8) "test.png"
     *      ["extension"]=>
     *      string(3) "png"
     *      ["filename"]=>
     *      string(4) "test"
     * ]
     * @param string $filePath     图片的路径
     * @param int $width    图片的宽度
     * @param int $height   图片的高度
     * @param int $quality  图片的质量
     * @param string $type 0默认裁剪缩略图  1填充缩略图 $mode=ManipulatorInterface::THUMBNAIL_OUTBOUND
     */
    public static function thumbnail($filePath,$width = 100 , $height = 100,$quality = 100,$type = 0)
    {
        # 解析文件路径
        $fileInfo = pathinfo($filePath);

        #缩略图文件名称
        $filename = 'thumbnail_'.$width.'X'.$height.$fileInfo['basename'];
        $thumbnailName = $fileInfo['dirname'].DIRECTORY_SEPARATOR.$filename;
        $imgObj = empty($type) ? Image::thumbnail($filePath,$width,$height,ManipulatorInterface::THUMBNAIL_OUTBOUND) : Image::thumbnail($filePath,$width,$height,ManipulatorInterface::THUMBNAIL_INSET);
        # 生成一张裁剪模式100 x 100 的缩略图
        $imgObj->save($thumbnailName , ['quality' => $quality]);//生成缩略图的质量

        return $filename; # 返回缩略图的名称
    }

    /**
     * @param string $filename    裁剪的图片名
     * @param string $width 裁剪图片宽度
     * @param string $height 裁剪图片高度
     * @param array $start 起始点 x y
     */
    public static function crop($filename, $width, $height, array $start = [0, 0],$quality = 100)
    {
        # 解析文件路径
        $fileInfo = pathinfo($filename);

        #缩略图文件名称
        $thumbnailName = $fileInfo['dirname'].'/thumbnail_crop_'.$width.'X'.$height.$fileInfo['basename'];
        Image::crop($filename,$width,$height,$start)->save($thumbnailName,$quality);
    }

    /**
     * 生成水印图片
     * @param $filename
     * @param $watermarkFilename
     * @param array $start
     * @param int $quality
     */
    public static function water($filename, $watermarkFilename, array $start = [0, 0],$quality = 100)
    {
        # 解析文件路径
        $fileInfo = pathinfo($filename);

        #缩略图文件名称
        $thumbnailName = $fileInfo['dirname'].'/thumbnail_water_'.$fileInfo['basename'];
        Image::watermark($filename,$watermarkFilename,$start)->save($thumbnailName,$quality);
    }

    /**
     * @param $filename
     * @param $text
     * @param $fontFile
     * @param array $start
     *
     * @param array $fontOptions
     * - color: 文字颜色 默认 "fff".
     * - size: 文字大小 默认 12.
     * - angle: 文字的角度  默认 0.
     */
    public function waterText($filename, $text, $fontFile, array $start = [0, 0], array $fontOptions = [],$quality)
    {
        # 解析文件路径
        $fileInfo = pathinfo($filename);

        #缩略图文件名称
        $thumbnailName = $fileInfo['dirname'].'/thumbnail_water_'.$fileInfo['basename'];
        Image::text($filename,$text,$fontFile,$start,$fontOptions)->save($thumbnailName,$quality);
    }
}